package org.myontology.html;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Tag {

	
	private String tagName;
	private boolean hasContent=false;
	private Map<String, String> parameters = new HashMap<String, String>();
	
	public Tag(String tagName) {
		this.tagName=tagName;
		builder.append("<"+tagName);
	}
	
	public String close() {
		closeTag();
		builder.append("</"+tagName+">");
		return builder.toString();
	}

	public String getId() {
		return getParameter("id");
	}
	
	public String getParameter(String name) {
		String val = parameters.get(name);
		return val==null?"":val;
	}
	
	private void closeTag() {
		if (!hasContent) {
			
			for (Iterator<Entry<String, String>> it = parameters.entrySet().iterator();it.hasNext();) {
				Entry<String, String> e = it.next();
				writeParameter(e.getKey(), e.getValue());
			}
			builder.append(">");
		}
	}
	
	public Tag content(Tag content) {
		return content(content.close());
	}
	
	public Tag content(String content) {
		
		closeTag();
		hasContent=true;
		builder.append(content);
		return this;
	}
	
	StringBuilder builder = new StringBuilder();
	
	public Tag parameter(String name,String value) {
		name = name.toLowerCase();
		String oldValue= parameters.get(name);
		if (oldValue==null)
			oldValue="";
		oldValue+=value;
		parameters.put(name, oldValue);
		return this;
	}
	
	private Tag writeParameter(String name,String value) {
		builder.append(" "+name+"=\""+value+"\" ");
		return this;
	}
	
	public Tag name(String value) {
		return parameter("name", value);
	}
	
	public Tag style(String value) {
		return parameter("style", value);
	}
	
	public Tag onFocus(String value) {
		return parameter("onFocus", value);
	}
	public Tag tabindex(String value) {
		return parameter("tabindex", value);
	}
	public Tag id(String value) {
		return parameter("id", value);
	}
	public Tag type(String value) {
		return parameter("type", value);
	}
	public Tag onblur(String value) {
		return parameter("onblur", value);
	}
	public Tag onkeyup(String value) {
		return parameter("onkeyup", value);
	}
	public Tag onClick(String value) {
		return parameter("onclick", value);
	}
	public Tag onkeypress(String value) {
		return parameter("onkeypress", value);
	}
	public Tag onmouseover(String value) {
		return parameter("onmouseover", value);
	}
	public Tag value(String value) {
		return parameter("value", value);
	}
	public Tag clazz(String value) {
		return parameter("class", value);
	}
	public Tag disabled(boolean value) {
		return parameter("disabled", value+"");
	}
	public Tag src(String value) {
		return parameter("src", value);
	}
	public Tag border(String value) {
		return parameter("border", value);
	}
	
	public static String valueById(String id) {
		return elementById(id)+".value";
	}
	
	public static String elementById(String id) {
		return " document.getElementById('"+id+"')";
	}
	
	public static String script(String... vals) {
		StringBuilder sb = new StringBuilder();
		for (int i=0;i<vals.length;i++) {
			if (i==0) {
				sb.append(vals[i]).append("(");
			}
			else {
				if (vals[i].length()==0 || !vals[i].substring(0,1).equals(" ")) {
					sb.append("'");
				}
				sb.append(vals[i]);
				if (vals[i].length()==0 || !vals[i].substring(0,1).equals(" ")) {
					sb.append("'");
				}
				if (i<vals.length-1)
					sb.append(",");
			}
		}
		sb.append(");");
		return sb.toString();
	}
	
}
